package com.yyx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyx.common.R;
import com.yyx.common.exception.CustomException;
import com.yyx.entity.Category;
import com.yyx.entity.Dish;
import com.yyx.entity.Setmeal;
import com.yyx.mapper.CategoryMapper;
import com.yyx.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyx.service.DishService;
import com.yyx.service.SetmealDishService;
import com.yyx.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author yeyinxuan
 * @since 2023-07-31
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
     DishService dishService;
    @Autowired
    SetmealService setmealService;

    @Override
    public Boolean saveCategory(Category category) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Category::getName,category.getName());
        Category category1 = this.getOne(lambdaQueryWrapper);
        if(category1 != null){
            return false;
        }
        return true;
    }

    //    @Autowired
//    CategoryService categoryService;
    @Override
    public Page selectpage(int page, int pageSize) {
//        分页构造器
        Page page1 = new Page(page,pageSize);
//        条件构造器
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.orderByAsc(Category::getType,Category::getSort);

        this.page(page1,lambdaQueryWrapper);

        return page1;
    }

    public Boolean delcetCategoryByid(Long id){
//        判断该分类有没有关联菜品
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,id);

        int count1 = dishService.count(lambdaQueryWrapper);
        if(count1 > 0){
          throw new CustomException("该分类关联了菜品，无法删除");
        }
//        判断该分类有没有关联套餐
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(Setmeal::getCategoryId,id);

        int count2 = setmealService.count(lambdaQueryWrapper1);
        if(count2 > 0){
           throw new CustomException("该分类关联了套餐，无法删除");
        }
        if(count1 == 0 && count2 == 0){
            this.removeById(id);
            return true;
        }
    return false;

    }

//    @Override
//    public Boolean updateCategory(Category category) {
//
//        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
//        lambdaQueryWrapper.notIn(Category::getId,category.getId());
//        lambdaQueryWrapper.eq(Category::getName,category.getName());
//        Category category1 = this.getOne(lambdaQueryWrapper);
//        if(category1 != null){
//            return false;
//        }
//        this.save(category);
//        return true;
//    }

    public List listType(Category category){
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(category.getType()!= null, Category::getType,category.getType());
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = this.list(lambdaQueryWrapper);
        return list;
    }

}
